var saoptions = {
		dataType : "json",
		beforeSubmit: function(arr, $form, options) { 
			for ( var i = 0; i < arr.length; i++) {
				if( arr[i].name=="startTime" ){
					arr[i].value = seldate + " " + arr[i].value;
				}
				else if( arr[i].name=="endTime" ){
					arr[i].value = seldate + " " + arr[i].value;
				}
			}
			/*if( arr[8].value.indexOf("-")<0 ){
				arr[8].value = seldate + " " + arr[8].value;
			}
			if( arr[9].value.indexOf("-")<0 ){
				arr[9].value = seldate + " " + arr[9].value;
			}*/
			return $("#savea").valid();
		},
		success : function(data){
			
			var darr = seldate.split("-");
			var st = $("#startTime").val();
			var et = $("#endTime").val();
			var starr;
			if( st.indexOf(" ")>-1 ){
				var stmp = st.split(" ");
				starr = stmp[1].split(":");
			}
			else{
				starr = st.split(":");
			}
			var etarr;
			if( et.indexOf(" ")>-1 ){
				var etmp = et.split(" ");
				etarr = etmp[1].split(":");
			}
			else{
				etarr = et.split(":");
			}
			
			var teach = $("#teacherId").find("option:selected").text().split(",");
			var event = {
					title: data[1][0].title,
					tooltip: $("#placeid").find("option:selected").text()+"_"+$("#classesId").find("option:selected").text()+"_"+$("#classcourseid").val()+"，No."+$("#number").val()+"，"+teach[0],
					color: data[1][0].color,
					textColor: data[1][0].textColor,
					allDay: false,
					start: new Date(darr[0], darr[1]*1-1, darr[2], starr[0]*1, starr[1]*1),
					end: new Date(darr[0], darr[1]*1-1, darr[2], etarr[0]*1, etarr[1]*1),
					id: data[1][0].id
				};
			
			if(data!=null && data[0]=="add"){
				var isArrangeLaw = false;
				var xingqi = "";
				var az = $("#savea :checkbox");
				az.each(function(i,box){
					//var av = box.checked;
					if(box.checked==true){
						isArrangeLaw = true;
						xingqi = xingqi + box.value;
					}
				});
				if(isArrangeLaw){
					var evs = new Array();
					//var cnr = $("#classcourseid").val();
					var stdate = new Date(darr[0], darr[1]*1-1, darr[2], starr[0]*1, starr[1]*1);
					var endate = new Date(darr[0], darr[1]*1-1, darr[2], etarr[0]*1, etarr[1]*1);
					var cnum = $("#number").val();
					
					for ( var i = 0; i < data[1].length; ) {
						var tday = stdate.getDay()*1+1;
						if( xingqi.indexOf(tday)>-1 ){
							var lawEvent = {
									title: data[1][i].title,
									tooltip: $("#placeid").find("option:selected").text()+"_"+$("#classesId").find("option:selected").text()+"_"+$("#classcourseid").val()+"，No."+cnum+"，"+teach[0],
									color: data[1][i].color,
									textColor: data[1][i].textColor,
									allDay: false,
									start: data[1][i].start,
									end: data[1][i].end,
									id: data[1][i].id
								};
							evs.push(lawEvent);
							i++;
							cnum++;
						}
						stdate = new Date( stdate.getTime() + 86400000 );
						endate = new Date( endate.getTime() + 86400000 );
					}
					$('#calendar').fullCalendar( 'addEventSource', evs );
				}
				else{
					$('#calendar').fullCalendar( 'addEventSource', [event] ); 
				}
			}
			else if(data!=null && data[0]=="update"){
				updevent.title = event.title;
				updevent.color = event.color;
				updevent.start = event.start;
				updevent.end = event.end;
				
				$('#calendar').fullCalendar( 'updateEvent', updevent );
				
			}
			$("#arrange").dialog("close");
			if(!iscopyarr){
				$('#placeid,#classesId,#classcourseid,#teacherId,:checkbox').clearFields();
			}
		}
		
	};

$(function() {
	
	$("#savea").validate({
		rules : {
			placid : {required : true},
			classesId : {required : true},
			classcourseid : {required : true},
			startTime : {required : true},
			endTime : {required : true}
		}
	});
	$("#saveclass").validate({
		rules : {
			courseId : {required : true},
			placeId : {required : true},
			className : {required : true}
		}
	});
	$("#savecourse").validate({
		rules : {
			name : {required : true},
			classHour : {required : true}
		}
	});
	$("#saveplace").validate({
		rules : {
			name : {required : true}
		}
	});
	$("#savet").validate({
		rules : {
			name : {required : true}
		}
	});
	$("#saveaccount").validate({
		rules : {
			name : {required : true},
			loginName : {required : true},
			checkpass : {equalTo : "#userpass"}
		}
	});
	
	$("#savet,#savecourse,#saveplace,#saveclass,#saveaccount").ajaxForm({
		dataType : "json",
		beforeSubmit: function(arr, $form, options) { 
			var id = $form.attr("id");
			switch(id){
				case "savet":
					return $("#savet").valid();
					break;
				case "savecourse":
					return $("#savecourse").valid();
					break;
				case "saveclass":
					return $("#saveclass").valid();
					break;
				case "saveplace":
					return $("#saveplace").valid();
					break;
				case "saveaccount":
					return $("#saveaccount").valid();
					break;
			}
			
		},
		success : function(data){
			if(data){
				alert("保存成功！");
				if( $( "#tjkc" ).dialog( "isOpen" ) ){
					var treeObj = $.fn.zTree.getZTreeObj("coursetree");
					treeObj.reAsyncChildNodes(null, "refresh");
				}
				else if ( $( "#kb" ).dialog( "isOpen" ) ) {
					classAndPlace.put(data[1][0],data[1][1]);
				}
				$("#addteacher,#tjkc,#tjdd,#kb,#accountm").dialog("close");
				$('#savet,#savecourse,#savecourse').clearForm();
			}
		}
	});
	
	$('#savea').submit(function() { 
        $(this).ajaxSubmit(saoptions); 
        return false; 
    }); 
});